#include "game.h"

CGame::CGame( HINSTANCE hInstance,int nWidth,int nHeight )
	:m_nWidth(nWidth),m_nHeight(nHeight)
{
	
	m_engine=new CEngine( hInstance,m_nWidth,m_nHeight,TRUE,this );
	if( !m_engine->Init() )
	{
		MessageBox( NULL,L"m_engine->Init() - FAILED",L"ERROR",MB_OK );
		return;
	}

	
	CModel::SetDevice( m_engine->m_device );

	m_input=new CInput( hInstance,m_engine->m_hWnd );
	if( !m_input->InitKeyboard() )
	{
		MessageBox( NULL,L"m_input->InitKeyboard() - FAILED",L"ERROR",MB_OK );
		return;
	}
	if( !m_input->InitMouse() )
	{
		MessageBox( NULL,L"m_input->InitMouse() - FAILED",L"ERROR",MB_OK );
		return;
	}
	CEngine::s_lpInput=m_input;

	m_hWnd=m_engine->m_hWnd;

	m_draw=new CDraw( m_engine->m_device,m_hWnd,m_nWidth,m_nHeight );

	if( !m_draw->Init() )
	{
		MessageBox( NULL,L"m_draw->Init() - FAILED",L"ERROR",MB_OK );
		return;
	}

	m_sound=new CSound( m_engine->m_device,m_hWnd );
	m_sound->Init();


	m_modeDebug=new CDebugMode( m_input,m_engine,m_draw,m_sound,this );
	m_modePlay=new CPlayMode( m_input,m_engine,m_draw,m_sound,this );

	m_mode=m_modePlay;

}


CGame::~CGame()
{
	m_mode=NULL;

	delete m_modeDebug;
	m_modeDebug=NULL;

	delete m_draw;
	m_draw=NULL;

	delete m_input;
	m_input=NULL;
	CEngine::s_lpInput=NULL;

	delete m_engine;
	m_engine=NULL;

	delete m_sound;
	m_sound=NULL;

}


VOID CGame::Runtime( DWORD dwNow,DWORD dwFPS )
{
	m_mode->Control();
	m_mode->Draw( dwNow,dwFPS );
}


VOID CGame::Run( DWORD dwDelay )
{
	m_engine->MessageLoop(dwDelay);
}


VOID CGame::Exit()
{
	::DestroyWindow( m_hWnd );
}

